<template>
  <div>
    <el-card class="card">
      <div slot="header" class="clearfix">
        <span>条件查询</span>
      </div>
      <div class="text-main">
        <el-row :gutter="10" type="flex" justify="center">

          <el-form :inline="true" class="demo-form-inline" :model="searchList">
            <el-form-item label="作者">
              <el-input v-model="searchList.name" size="medium" placeholder="作者" />
            </el-form-item>
            <el-form-item label="标题">
              <el-input v-model="searchList.title" size="medium" placeholder="标题" />
            </el-form-item>
            <el-form-item label="类型">
              <el-select v-model="searchList.select" size="medium" placeholder="请选择">
                <el-option label="富文本" :value="0" />
                <el-option label="MarhDown" :value="1" />
              </el-select>
            </el-form-item>
          </el-form>
        </el-row>
        <el-row :gutter="10" type="flex" justify="end">
          <el-col :span="6"><el-button size="mini" @click="reset">重置</el-button>
            <el-button type="primary" size="mini" @click="dosearch">搜索</el-button>
          </el-col>
        </el-row>
      </div>
    </el-card>

    <!-- 列表 -->
    <el-card class="list-card">
      <div slot="header" class="add">
        <el-button style="float: left" size="mini" type="primary" @click="add">新增</el-button>
      </div>

      <el-table v-loading="loading" :data="list" border style="width: 100%" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0.8)">
        <el-table-column type="index" width="50" fixed="left" align="center" />
        <el-table-column prop="title" label="标题" align="center" width="250" />
        <el-table-column label="图片" align="center" width="200">
          <template slot-scope="scope">
            <img class="list_img" :src="scope.row.coverImg" style="width:100px;height:100px">
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="添加时间" align="center" width="300" />
        <el-table-column prop="author" label="作者" align="center" width="200" />
        <!-- 是否展示 -->
        <el-table-column label="是否展示" align="center" width="200">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.isShow" :inactive-value="0" :active-value="1" @change="changeStatus(scope.row)" />
          </template>
        </el-table-column>
        <el-table-column label="内容" align="center" width="200">
          <template slot-scope="scope">
            <el-tag>{{ scope.row.editorType == 0 ? '富文本' : 'MarkDown' }}</el-tag>
          </template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column label="操作" align="center" fixed="right" width="150">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="edit(scope.row)">编辑</el-button>
            <el-button type="text" size="mini" @click="del(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        :current-page="page.currentPage"
        :page-sizes="page.pageSizes"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>
<script>
import { findArticles, del, changeShowStatus } from '@/api/content/article'
export default {
  name: 'Article',
  data() {
    return {
      loading: false,
      searchList: {
        name: '',
        title: '',
        select: ''
      },
      list: [],
      page: {
        currentPage: 1,
        pageSizes: [10, 20, 30],
        pageSize: 10,
        total: 0
      }
    }
  },
  computed: {},
  created() {
    this.getArticles()
  },
  methods: {
    // 查找
    dosearch() {
      // this.$options.data()是该实例最初的data
      this.page = this.$options.data().page
      this.getArticles()
    },
    // 重置
    reset() {
      this.searchList = this.$options.data().searchList
    },
    add() {
      this.$router.push({ name: 'ArticleDetail' })
    },
    // 改变状态
    changeStatus(val) {
      var nowStatus = val.isShow === 1 ? 0 : 1
      changeShowStatus({
        id: val.id,
        isShow: val.isShow
      }).then(res => {
        // console.log(res)
        if (res.success) {
          this.$message.success('修改成功')
        } else {
          this.$message.error('修改失败')
          val.isShow = nowStatus
        }
      })
    },
    // 编辑
    edit(row) {
      this.$router.push({ name: 'EditArticle', params: { id: row.id }})
    },
    del(row) {
      this.$confirm('此操作将永久删除该数据', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(row.id).then(res => {
          if (res.success) {
            this.$message.success('删除成功')
            this.getArticles()
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getArticles()
    },
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.getArticles()
    },
    getArticles() {
      this.loading = true
      findArticles(this.page.currentPage, this.page.pageSize, { ...this.searchList }).then((res) => {
        console.log(res)
        if (res.success) {
          this.list = res.data.rows
          this.page.total = res.data.total
        } else {
          this.$message('获取文章列表失败')
        }
      })
      this.loading = false
    }
  }
}
</script>
<style lang="scss" scoped>
.card {
  margin: 30px;
  border: 1px solid #ebeef5;
  .text-main {
    padding: 20px;
    color: #303133;
      .text {
        width: 200px;
      }
  }
}
.list-card{
  margin: 30px;
  border-radius: 4px;
  color: #303133;
  background-color: #fff;
  border: 1px solid #ebeef5;

  .add{
    padding: 18px 20px;
  }
}
</style>

